use std::convert::Infallible;

use crate::api::prelude::*;
use async_trait::async_trait;
use thiserror::Error;
use validator::impls::*;

/// aaa
#[derive(Debug, ApiModel, Serialize, Deserialize, Validator)]
pub struct Req {
    /// bbb
    #[name("id")]
    pub id: i32,

    /// 真实姓名
    #[name("姓名")]
    #[validate(length(2, 10))]
    pub name: Option<String>,
}

#[derive(Debug, ApiModel, Serialize, Deserialize, Validator)]
pub struct Res {
    /// 订单
    #[name("aaa")]
    pub order_no: String,
}

#[derive(Debug, ApiError, Error)]
pub enum Error {
    #[status_code(StatusCode::INTERNAL_SERVER_ERROR)]
    #[error("{0}")]
    ServerError(anyhow::Error),
}

#[api]
#[async_trait]
pub trait TaskApi {
    #[put]
    async fn get_by_id(self, req: Json<Req>) -> Result<Res, Infallible>;

    #[post]
    async fn update_user(self, req: Json<Req>) -> Result<Res, Error>;
}
